local zhemu = fk.CreateSkill {
  name = "emo__zhemu",
}

zhemu:addEffect(fk.Damaged, {
  anim_type = "masochism",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(zhemu.name) and data.from
    and data.from ~= player and (not data.from.dead) and not data.from:isNude()
  end,
  on_cost = function(self, event, target, player, data)
    if player.room:askToSkillInvoke(player, {skill_name = zhemu.name, prompt = "#emo__zhemu-invoke:"..data.from.id}) then
      event:setCostData(self, {tos = {data.from}})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local from = data.from---@type ServerPlayer
    for _ = 1, 2 do
      if from:isNude() or from.dead or player.dead then return false end
      local cid = room:askToChooseCard(player, {target = from, skill_name = zhemu.name, flag = "he"})
      room:setCardMark(Fk:getCardById(cid), zhemu.name, player.id)
      room:moveCards({
        ids = {cid},
        from = from,
        toArea = Card.DrawPile,
        moveReason = fk.ReasonPut,
        skillName = zhemu.name,
        drawPilePosition = math.random(1, #room.draw_pile),
      })
    end
  end
})

zhemu:addEffect(fk.AfterCardsMove, {
  anim_type = "defensive",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if not player.dead then
      for _, move in ipairs(data) do
        for _, info in ipairs(move.moveInfo) do
          local card = Fk:getCardById(info.cardId)
          if info.fromArea == Card.DrawPile and card:getMark(zhemu.name) == player.id then
            return true
          end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      for _, info in ipairs(move.moveInfo) do
        local card = Fk:getCardById(info.cardId)
        if info.fromArea == Card.DrawPile and card:getMark(zhemu.name) == player.id then
          room:setCardMark(card, zhemu.name, 0)
        end
      end
    end
    if player:isWounded() then
      room:recover { num = 1, skillName = zhemu.name, who = player, recoverBy = player}
    end
  end
})


Fk:loadTranslationTable{
  ["emo__zhemu"] = "折木",
  [":emo__zhemu"] = "每当你受到其他角色造成的伤害后，你可以将该角色的两张牌依次洗入牌堆。当以此法洗入牌堆的牌离开牌堆时，你回复1点体力。",
  ["#emo__zhemu-invoke"] = "折木：你可以将 %src 的两张牌洗入摸牌堆",
}

return zhemu
